/* USER CODE BEGIN Header */
/**
 ******************************************************************************
 * @file           : main.c
 * @brief          : Main program body
 ******************************************************************************
 * @attention
 *
 * Copyright (c) 2024 STMicroelectronics.
 * All rights reserved.
 *
 * This software is licensed under terms that can be found in the LICENSE file
 * in the root directory of this software component.
 * If no LICENSE file comes with this software, it is provided AS-IS.
 *
 ******************************************************************************
 */
/* USER CODE END Header */
/* Includes ------------------------------------------------------------------*/
#include "main.h"
#include "usb_device.h"
#include "key.h"
#include "usart.h"

#define KEY_ARROW_DOWN 0x51
#define KEY_ARROW_UP 0x52
#define KEY_ARROW_LEFT 0x50
#define KEY_ARROW_RIGHT 0x4F
#define KEY_SPACE_BAR 0x2C
//
#define FIRST_PRESSED_KEY 2
#define SECOND_PRESSED_KEY 3

/* Private includes ----------------------------------------------------------*/
/* USER CODE BEGIN Includes */

/* USER CODE END Includes */

/* Private typedef -----------------------------------------------------------*/
/* USER CODE BEGIN PTD */

/* USER CODE END PTD */

/* Private define ------------------------------------------------------------*/
/* USER CODE BEGIN PD */

/* USER CODE END PD */

/* Private macro -------------------------------------------------------------*/
/* USER CODE BEGIN PM */
/* USER CODE END PM */

/* Private variables ---------------------------------------------------------*/

/* USER CODE BEGIN PV */

/* USER CODE END PV */

/* Private function prototypes -----------------------------------------------*/
void SystemClock_Config(void);
static void MX_GPIO_Init(void);
/* USER CODE BEGIN PFP */

/* USER CODE END PFP */

/* Private user code ---------------------------------------------------------*/
/* USER CODE BEGIN 0 */

/* USER CODE END 0 */


extern USBD_HandleTypeDef hUsbDeviceFS;
extern uint8_t USBD_HID_SendReport(USBD_HandleTypeDef *pdev, uint8_t *report, uint16_t len);
//

void ExecuteHellDiver2CMD(uint8_t *Key_Sequence,size_t Key_Sequence_len ,uint8_t * usbReprtBuf)
{
  usbReprtBuf[FIRST_PRESSED_KEY] = KEY_SPACE_BAR;
  USBD_HID_SendReport(&hUsbDeviceFS, usbReprtBuf, 8);
  HAL_Delay(100);
  for(size_t i = 0; i < Key_Sequence_len ; i++)
  {
    usbReprtBuf[SECOND_PRESSED_KEY] = Key_Sequence[i];
    USBD_HID_SendReport(&hUsbDeviceFS, usbReprtBuf, 8);
    HAL_Delay(80);
    usbReprtBuf[SECOND_PRESSED_KEY] = 0;
    USBD_HID_SendReport(&hUsbDeviceFS, usbReprtBuf, 8);
    HAL_Delay(80);
  }
  usbReprtBuf[SECOND_PRESSED_KEY] = 0;
  usbReprtBuf[FIRST_PRESSED_KEY] = 0;
  USBD_HID_SendReport(&hUsbDeviceFS, usbReprtBuf, 8);
}
int main(void)
{
  uint8_t key = 0;
  /* USER CODE BEGIN 1 */

  /* USER CODE END 1 */

  /* MCU Configuration--------------------------------------------------------*/

  /* Reset of all peripherals, Initializes the Flash interface and the Systick. */
  HAL_Init();

  /* USER CODE BEGIN Init */

  /* USER CODE END Init */

  /* Configure the system clock */
  SystemClock_Config();

  /* USER CODE BEGIN SysInit */

  /* USER CODE END SysInit */

  /* Initialize all configured peripherals */
  MX_GPIO_Init();
  MX_USB_DEVICE_Init();
  KEY_Init();
  uart_init(115200); // 初始化串口，波特率为115200
  /* USER CODE BEGIN 2 */
  uint8_t buff[8] = {0, 0, 0, 0, 0, 0, 0, 0}; // 首字节为ctrl等特殊按键情况，第二字节永远为0，第三字节为按下的其他按键
  // 支援武器
  uint8_t EAT_KeySequence[] = {KEY_ARROW_DOWN, KEY_ARROW_DOWN, KEY_ARROW_LEFT, KEY_ARROW_UP, KEY_ARROW_RIGHT}; // 反坦克炮
  uint8_t MG43_KeySequence[] = {KEY_ARROW_DOWN, KEY_ARROW_LEFT, KEY_ARROW_DOWN, KEY_ARROW_UP, KEY_ARROW_RIGHT}; // 机枪
  uint8_t APW1_KeySequence[] = {KEY_ARROW_DOWN, KEY_ARROW_LEFT, KEY_ARROW_RIGHT, KEY_ARROW_UP, KEY_ARROW_DOWN}; // 反器材步枪
  uint8_t AC8_KeySequence[] = {KEY_ARROW_DOWN, KEY_ARROW_LEFT, KEY_ARROW_DOWN, KEY_ARROW_UP, KEY_ARROW_UP,KEY_ARROW_RIGHT}; // 机炮
  uint8_t M105_KeySequence[] = {KEY_ARROW_DOWN, KEY_ARROW_LEFT, KEY_ARROW_DOWN, KEY_ARROW_UP, KEY_ARROW_UP,KEY_ARROW_LEFT}; // 盟友
  uint8_t ARC3_KeySequence[] = {KEY_ARROW_DOWN, KEY_ARROW_RIGHT, KEY_ARROW_DOWN, KEY_ARROW_UP, KEY_ARROW_LEFT,KEY_ARROW_LEFT}; // 电弧
  uint8_t LAS99_KeySequence[] = {KEY_ARROW_DOWN,KEY_ARROW_DOWN,KEY_ARROW_UP,KEY_ARROW_LEFT,KEY_ARROW_RIGHT}; // 类星体加农炮
  // 进攻型飞鹰
  uint8_t Egale500KG_KeySequence[] = {KEY_ARROW_UP,KEY_ARROW_RIGHT,KEY_ARROW_DOWN,KEY_ARROW_DOWN,KEY_ARROW_DOWN}; // 飞鹰500kg炸弹
  // 进攻型轨道武器
  uint8_t OrbitalLaser_KeySequence[] = {KEY_ARROW_RIGHT,KEY_ARROW_DOWN,KEY_ARROW_UP,KEY_ARROW_RIGHT,KEY_ARROW_DOWN}; // 轨道激光

  /* USER CODE END 2 */
  /* Infinite loop */ 
  /* USER CODE BEGIN  WHILE */
  while (1)
  {

    key = KEY_Scan(0);
    /* USER CODE END WHILE */
      if (key == KEY0_PRES)
      {
        ExecuteHellDiver2CMD(AC8_KeySequence,sizeof(AC8_KeySequence)/sizeof(uint8_t),buff);
      }
      else if(key == KEY1_PRES)
      {
        ExecuteHellDiver2CMD(ARC3_KeySequence,sizeof(ARC3_KeySequence)/sizeof(uint8_t),buff);
      }
      else if(key == KEY2_PRES)
      {
        ExecuteHellDiver2CMD(OrbitalLaser_KeySequence,sizeof(OrbitalLaser_KeySequence)/sizeof(uint8_t),buff);
      }
      else if(key == WKUP_PRES)
      {
        ExecuteHellDiver2CMD(Egale500KG_KeySequence,sizeof(Egale500KG_KeySequence)/sizeof(uint8_t),buff);
      }
  }
  /* USER CODE END 3 */
}

/**
 * @brief System Clock Configuration
 * @retval None
 */
void SystemClock_Config(void)
{
  RCC_OscInitTypeDef RCC_OscInitStruct = {0};
  RCC_ClkInitTypeDef RCC_ClkInitStruct = {0};

  /** Configure the main internal regulator output voltage
   */
  if (HAL_PWREx_ControlVoltageScaling(PWR_REGULATOR_VOLTAGE_SCALE1) != HAL_OK)
  {
    Error_Handler();
  }

  /** Initializes the RCC Oscillators according to the specified parameters
   * in the RCC_OscInitTypeDef structure.
   */
  RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_MSI;
  RCC_OscInitStruct.MSIState = RCC_MSI_ON;
  RCC_OscInitStruct.MSICalibrationValue = 0;
  RCC_OscInitStruct.MSIClockRange = RCC_MSIRANGE_6;
  RCC_OscInitStruct.PLL.PLLState = RCC_PLL_ON;
  RCC_OscInitStruct.PLL.PLLSource = RCC_PLLSOURCE_MSI;
  RCC_OscInitStruct.PLL.PLLM = 1;
  RCC_OscInitStruct.PLL.PLLN = 40;
  RCC_OscInitStruct.PLL.PLLP = RCC_PLLP_DIV7;
  RCC_OscInitStruct.PLL.PLLQ = RCC_PLLQ_DIV2;
  RCC_OscInitStruct.PLL.PLLR = RCC_PLLR_DIV2;
  if (HAL_RCC_OscConfig(&RCC_OscInitStruct) != HAL_OK)
  {
    Error_Handler();
  }

  /** Initializes the CPU, AHB and APB buses clocks
   */
  RCC_ClkInitStruct.ClockType = RCC_CLOCKTYPE_HCLK | RCC_CLOCKTYPE_SYSCLK | RCC_CLOCKTYPE_PCLK1 | RCC_CLOCKTYPE_PCLK2;
  RCC_ClkInitStruct.SYSCLKSource = RCC_SYSCLKSOURCE_PLLCLK;
  RCC_ClkInitStruct.AHBCLKDivider = RCC_SYSCLK_DIV1;
  RCC_ClkInitStruct.APB1CLKDivider = RCC_HCLK_DIV1;
  RCC_ClkInitStruct.APB2CLKDivider = RCC_HCLK_DIV1;

  if (HAL_RCC_ClockConfig(&RCC_ClkInitStruct, FLASH_LATENCY_4) != HAL_OK)
  {
    Error_Handler();
  }
}

/**
 * @brief GPIO Initialization Function
 * @param None
 * @retval None
 */
static void MX_GPIO_Init(void)
{
  /* USER CODE BEGIN MX_GPIO_Init_1 */
  /* USER CODE END MX_GPIO_Init_1 */

  /* GPIO Ports Clock Enable */
  __HAL_RCC_GPIOA_CLK_ENABLE();

  /* USER CODE BEGIN MX_GPIO_Init_2 */
  /* USER CODE END MX_GPIO_Init_2 */
}

/* USER CODE BEGIN 4 */

/* USER CODE END 4 */

/**
 * @brief  This function is executed in case of error occurrence.
 * @retval None
 */
void Error_Handler(void)
{
  /* USER CODE BEGIN Error_Handler_Debug */
  /* User can add his own implementation to report the HAL error return state */
  __disable_irq();
  while (1)
  {
  }
  /* USER CODE END Error_Handler_Debug */
}

#ifdef USE_FULL_ASSERT
/**
 * @brief  Reports the name of the source file and the source line number
 *         where the assert_param error has occurred.
 * @param  file: pointer to the source file name
 * @param  line: assert_param error line source number
 * @retval None
 */
void assert_failed(uint8_t *file, uint32_t line)
{
  /* USER CODE BEGIN 6 */
  /* User can add his own implementation to report the file name and line number,
     ex: printf("Wrong parameters value: file %s on line %d\r\n", file, line) */
  /* USER CODE END 6 */
}
#endif /* USE_FULL_ASSERT */
